package com.weitu.observation.observationCard.service.impl;

import com.weitu.observation.observationCard.entity.ObservationCard;
import com.weitu.observation.observationCard.entity.ObservationCardOpinionCreate;
import com.weitu.observation.observationCard.entity.ObservationCardOptionAnalysis;
import com.weitu.observation.observationCard.mapper.ObservationCardMapper;
import com.weitu.observation.observationCard.service.ObservationCardService;
import com.weitu.permission.department.entity.Department;
import com.weitu.permission.department.mapper.DepartmentMapper;
import com.weitu.permission.dictionary.entity.Dictionary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/4/1.
 */
@Service
public class ObservationCardServiceImpl implements ObservationCardService{

    private final ObservationCardMapper observationCardMapper;

    private final DepartmentMapper departmentMapper;

    @Autowired
    public ObservationCardServiceImpl(ObservationCardMapper observationCardMapper,DepartmentMapper departmentMapper){
        this.observationCardMapper=observationCardMapper;
        this.departmentMapper=departmentMapper;
    }

    @Override
    public List<ObservationCard> queryObservationList(Map<String, Object> map) {

        toDeptIds(map);

        return this.observationCardMapper.queryObservationList(map);

    }
    private void toDeptIds(Map<String, Object> map){
        String deptId=map.get("deptId").toString();
        List<String> deptIds=new ArrayList<String>();
        addDeptId(deptIds,deptId);
        map.put("deptIds",deptIds);
    }
    private void addDeptId(List<String> deptIds,String deptId){
        deptIds.add(deptId);
        List<Department> parentDepartmentList = this.departmentMapper.findByParentId(deptId);
        if(parentDepartmentList.size()>0)
            for (Department department : parentDepartmentList) {
                String id = department.getId();
                addDeptId(deptIds,id);
            }
    }
    @Override
    public int queryCount(Map<String, Object> map) {
        toDeptIds(map);
        return this.observationCardMapper.queryCount(map);

    }

    @Override
    @Transactional
    public int insertCard(ObservationCard observationCard, List<ObservationCardOpinionCreate> list) {
        if(list.size()>0)
        this.observationCardMapper.insertCardOpinion(list);
        return this.observationCardMapper.insertCard(observationCard);
    }

    @Override
    @Transactional
    public int updateCard(ObservationCard observationCard, List<ObservationCardOpinionCreate> list, String id) {
        this.observationCardMapper.deleteCardOpinion(id);
        if(list.size()>0)
        this.observationCardMapper.insertCardOpinion(list);
        return this.observationCardMapper.updateCard(observationCard);
    }

    @Override
    public ObservationCard selectById(String id) {
        return this.observationCardMapper.selectById(id);
    }

    @Override
    public  List<Dictionary> selectCardPrompt(String str) {
        return this.observationCardMapper.selectCardPrompt(str);
    }

    @Override
    public List<ObservationCardOpinionCreate> selectOpinionIdByCardId(String cardId) {
        return this.observationCardMapper.selectOpinionIdByCardId(cardId);
    }

    @Override
    @Transactional
    public int analysisCard(ObservationCard observationCard, List<ObservationCardOptionAnalysis> list) {
        if(list.size()>0)
        this.observationCardMapper.insertCardOpinionAnalysis(list);
        return this.observationCardMapper.updateCard(observationCard);
    }

    @Override
    public int updateCard(ObservationCard observationCard) {
        return this.observationCardMapper.updateCard(observationCard);
    }

    @Override
    public List<ObservationCardOptionAnalysis> selectOptionAnalysisIdByCardId(String cardId) {
        return this.observationCardMapper.selectOptionAnalysisIdByCardId(cardId);
    }

    @Override
    public int deleteCard(String id) {
        return this.observationCardMapper.deleteCard(id);
    }
}
